package org.mybatis.generator.utils;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;

import java.util.Set;
import java.util.TreeSet;

/**
 * <b><code>JavaMapperGeneratorHelper</code></b>
 * <p/>
 * Description: 方法生成器
 * <p/>
 * <b>Creation Time:</b> 2020/06/08.
 *
 */
public class JavaMapperGeneratorHelper {

	/**
	 * java方法生成构造器.
	 * 
	 * @param methodName
	 * @param visibility
	 * @param returnJavaType
	 * @param parameters
	 * @return
	 */
	public static Method methodGenerator(String methodName, JavaVisibility visibility,
			FullyQualifiedJavaType returnJavaType, Parameter... parameters) {
		Method method = new Method();
		method.setName(methodName);
		method.setVisibility(visibility);
		method.setReturnType(returnJavaType);
		for (Parameter parameter : parameters) {
			method.addParameter(parameter);
		}
		return method;
	}

	/**
	 * 导入基础的java类型
	 * 
	 * @param introspectedTable
	 * @return
	 */
	public static Set<FullyQualifiedJavaType> importedBaseTypesGenerator(IntrospectedTable introspectedTable) {
		// 获取实体类类型
		FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
		// @Param需要导入的类型
		FullyQualifiedJavaType paramType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param");
		// Integer类型
		FullyQualifiedJavaType intInstance = FullyQualifiedJavaType.getIntInstance();
		// List<Entity>
		FullyQualifiedJavaType listParameterType = FullyQualifiedJavaType.getNewListInstance();

		Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
		importedTypes.add(parameterType);
		importedTypes.add(intInstance);
		importedTypes.add(paramType);
		importedTypes.add(listParameterType);
		return importedTypes;
	}

}